package com.example.rbacmanage.controller.background;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.Photo;
import com.example.rbacmanage.service.app.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/background/photo")
public class PhotoController {
    @Autowired
    private PhotoService photoService;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('background:photo:list')")
    public Result page(Photo photo){
        return Result.success(photoService.getPage(photo));
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id){
        return Result.success(photoService.getById(id));
    }

    @PreAuthorize("hasAnyAuthority('background:photo:add')")
    @PostMapping("/add")
    public Result add(@RequestBody Photo photo){
        photo.setCreateTime(new Date());
        photoService.save(photo);
        return Result.success();
    }
    @PreAuthorize("hasAnyAuthority('background:photo:update')")
    @PutMapping("/update")
    public Result update(@RequestBody Photo photo){
        photoService.update(photo);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('background:photo:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        photoService.remove(ids);
        return Result.success();
    }
}
